import os
from logging.handlers import RotatingFileHandler
import logging

from flask import Flask
from flask_login import LoginManager
from flask_mail import Mail

from config import Config  # 从config模块导入Config类
from flask_sqlalchemy import SQLAlchemy  # 从包里导入类
from flask_migrate import Migrate

app = Flask(__name__)
# app.config['SECRET_KEY'] = "I am a secret, you can't guess."
app.config.from_object(Config)  # 让Flask应用程序读取并应用config

login = LoginManager(app)  # 用于管理登录状态
login.login_view = 'login'  # 登录视图函数名称，

# 邮件
mail = Mail(app)

db = SQLAlchemy(app)  # 数据库对象
migrate = Migrate(app,db)  # 迁移引擎对象

from app import routes,models,errors


if not app.debug:
    if not os.path.exists("logs"):
        os.mkdir("logs")
    file_handler = RotatingFileHandler("logs/microblog.log",maxBytes=10240,backupCount=10)

    file_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
    file_handler.setLevel(logging.INFO)
    app.logger.addHandler(file_handler)


    app.logger.setLevel(logging.INFO)
    app.logger.info("Microblog startup")